package cn.iocoder.yudao.module.member.service.shunfengcheorder;


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.shunfengcheorder.ShunfengcheOrderConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.shunfengcheorder.ShunfengcheOrderDO;
import cn.iocoder.yudao.module.member.dal.mysql.shunfengcheorder.ShunfengcheOrderMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.SHUNFENGCHE_ORDER_NOT_EXISTS;

/**
 * 公众号顺风车订单 Service 实现类
 *
 * @author 路小强
 */
@Service
@Validated
public class ShunfengcheOrderServiceImpl implements ShunfengcheOrderService {

    @Resource
    private ShunfengcheOrderMapper shunfengcheOrderMapper;

    @Override
    public Long createShunfengcheOrder(ShunfengcheOrderCreateReqVO createReqVO) {
        // 插入
        ShunfengcheOrderDO shunfengcheOrder = ShunfengcheOrderConvert.INSTANCE.convert(createReqVO);
        shunfengcheOrderMapper.insert(shunfengcheOrder);
        // 返回
        return shunfengcheOrder.getId();
    }

    @Override
    public void updateShunfengcheOrder(ShunfengcheOrderUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateShunfengcheOrderExists(updateReqVO.getId());
        // 更新
        ShunfengcheOrderDO updateObj = ShunfengcheOrderConvert.INSTANCE.convert(updateReqVO);
        shunfengcheOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteShunfengcheOrder(Long id) {
        // 校验存在
        this.validateShunfengcheOrderExists(id);
        // 删除
        shunfengcheOrderMapper.deleteById(id);
    }

    private void validateShunfengcheOrderExists(Long id) {
        if (shunfengcheOrderMapper.selectById(id) == null) {
            throw exception(SHUNFENGCHE_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public ShunfengcheOrderDO getShunfengcheOrder(Long id) {
        return shunfengcheOrderMapper.selectById(id);
    }

    @Override
    public List<ShunfengcheOrderDO> getShunfengcheOrderList(Collection<Long> ids) {
        return shunfengcheOrderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShunfengcheOrderDO> getShunfengcheOrderPage(ShunfengcheOrderPageReqVO pageReqVO) {
        return shunfengcheOrderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShunfengcheOrderDO> getShunfengcheOrderList(ShunfengcheOrderExportReqVO exportReqVO) {
        return shunfengcheOrderMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean changeOrderStatus(Long id, int status) {
        LambdaUpdateWrapper<ShunfengcheOrderDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ShunfengcheOrderDO::getStatus, status);
        updateWrapper.eq(ShunfengcheOrderDO::getId, id).eq(ShunfengcheOrderDO::getStatus, 0);
        return shunfengcheOrderMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean reSend(Long id) {
        LambdaUpdateWrapper<ShunfengcheOrderDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ShunfengcheOrderDO::getStatus, 0);
        updateWrapper.eq(ShunfengcheOrderDO::getId, id);
        return shunfengcheOrderMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public int cancelTimeOutOrder() {
        LambdaUpdateWrapper<ShunfengcheOrderDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ShunfengcheOrderDO::getStatus, 2);
        updateWrapper.eq(ShunfengcheOrderDO::getStatus, 0);
        updateWrapper.lt(ShunfengcheOrderDO::getCreateTime, LocalDateTime.now().minusMinutes(30));
        return shunfengcheOrderMapper.update(null, updateWrapper);
    }

}
